package com.redheart.web.repository;

import com.redheart.web.entity.SysMenuEntity;
import com.redheart.web.entity.SysUserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface SysUserRepository extends JpaRepository<SysUserEntity,Long> {
    /**
     * 查询用户的所有权限
     * @param userId  用户ID
     */
    @Query(value = "\tselect m.perms from sys_user_role ur LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id LEFT JOIN sys_menu m on rm.menu_id = m.menu_id" +
            " where ur.user_id = ?1",nativeQuery = true)
    List<String> queryAllPerms(Long userId);

    /**
     * 按用户名查找
     * @param username
     * @return
     */
    SysUserEntity findByUsername(String username);

    /**
     * 查询用户的所有菜单ID
     * @param userId
     * @return
     */
    @Query(value = "select distinct rm.menu_id from sys_user_role ur LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id where ur.user_id = ?",nativeQuery = true)
    List<Long> queryAllMenuId(Long userId);

}

